<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hrdsurat extends CI_Controller {	
	
	public function index($kap='')
	{
		if(!$this->session->userdata('name')):
			redirect('auth');
		endif;
		
		$var['title']	 = 'HRD Surat';
		$var['kap']	 	 = $kap;
		$no = $this->session->userdata('no_hrdsurat');
		$kl = $this->session->userdata('klien_hrdsurat');
		$ta = $this->session->userdata('tgl_hrdsurat_awal');
		$tk = $this->session->userdata('tgl_hrdsurat_akhir');
		$var['hrdsurats']= $this->hrdsurats->getData(strtoupper($kap),$no,$kl,$ta,$tk);
		$var['content']  = 'index-hrdsurat';
		$this->load->view('index',$var);
	}
	
	function action(){
		if($this->session->userdata('no_surat')==''):
			$surat=array('no_hrdsurat'=>$this->input->post('no'),'klien_hrdsurat'=>$this->input->post('s'));
		else:
			$surat=array('no_hrdsurat'=>'','klien_hrdsurat'=>'');
		endif;
		$this->session->set_userdata($surat);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function date(){
		$start_date = $this->input->post('yyyy').'-'.$this->input->post('mm').'-'.$this->input->post('dd');
		$end_date   = $this->input->post('yyyy2').'-'.$this->input->post('mm2').'-'.$this->input->post('dd2');
		if($this->session->userdata('tgl_hrdsurat_awal')==''):
			$surat=array('tgl_hrdsurat_awal'=>$start_date,'tgl_hrdsurat_akhir'=>$end_date);
		else:
			$surat=array('tgl_hrdsurat_awal'=>'','tgl_hrdsurat_akhir'=>'');
		endif;
		$this->session->set_userdata($surat);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function excel(){
		//$this->db->use_table('surats');
		$this->db->select('no_surat', 'tgl');
	    // run joins, order by, where, or anything else here
		$this->db->order_by('tgl','DESC');
	    $query = $this->db->get('hrdsurats');
        to_excel($query,'Hrdsurats'); // filename is optional, without it, the plugin will default to 'exceloutput'
	}
	
	public function add($kap)
	{
		$var['title']	= 'Add HRD Surat';
		$var['kap']		= strtoupper($kap);
		$var['no']		= $this->hrdsurats->getLatestPK('no',$kap);
		$var['content'] = 'add-hrdsurat';
		$this->load->view('index',$var);
	}
	
	function save($kap){
		if($this->input->post('nama_klien')):
			$this->hrdsurats->getSave($kap);
		endif;
		redirect('hrdsurat/index/'.$kap,301);
	}
	
	public function edit($id,$kap)
	{
		$var['title']	= 'Edit HRD Surat';
		$var['kap']		= $kap;
		$var['hrd']		= $this->hrdsurats->getID($id);
		$var['no']		= $this->hrdsurats->getLatestPK('no',$kap);
		$var['content'] = 'edit-hrdsurat';
		$this->load->view('index',$var);
	}
	
	function update($kap){
		if($this->input->post('nama_klien')):
			$this->hrdsurats->getUpdate($kap);
		endif;
		redirect('hrdsurat/index/'.$kap,301);
	}
	
	function remove($id){
		if($id):
			$this->hrdsurats->getRemove($id);
		endif;
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
}
